//二叉树
{
    class BinarySearchTree {

        constructor() {

            this.root = null;
        }


        //二叉树插入操作
        insert(key) {

            var newNode = new Node(key);

            if (this.root === null) {

                this.root = newNode;

            } else {

                this._insertNode(this.root, newNode);
            }

        }

        //二叉树插入的辅助实现函数
        _insertNode(node, newNode) {

            if (newNode.key < node.key) {

                if (node.left === null) {
                    node.left = newNode;
                } else {
                    this._insertNode(node.left, newNode);
                }
            } else {

                if (node.right === null) {
                    node.right = newNode;
                } else {
                    this._insertNode(node.right, newNode);
                }
            }
        };

        //二叉树中序遍历
        inOrderTraverse(callBack) {

            // console.log(this.root);

            this._inOrderTraverseNode(this.root, callBack);
        };

        //二叉树先序排列的
        _inOrderTraverseNode(node, callBack) {

            if (node !== null) {

                this._inOrderTraverseNode(node.left, callBack);
                callBack(node.key);
                this._inOrderTraverseNode(node.right, callBack);

            }

        }

        //二叉树搜索最大值最小值
        min() {
            return this._min_max_node(this.root, 'min');
        }

        max() {
            return this._min_max_node(this.root, 'max');
        }

        //二叉树搜索最大最小值辅助函数
        _min_max_node(node, what) {

            if (what === 'min') {

                if (node) {
                    while (node && node.left !== null) {
                        node = node.left;
                    }
                    return node.key;
                }
                return null;

            } else if (what === 'max') {

                if (node) {
                    while (node && node.right !== null) {
                        node = node.right;
                    }

                    return node.key;
                }

                return null;

            }
        }


        //二叉树搜索一个特定的值
        search(key) {

            return this._search(this.root, key);

        }

        _search(node, key) {

            if (node === null) {
                return null;
            }

            if (key > node.key) {
                return this._search(node.right, key);
            } else if (key < node.key) {
                return this._search(node.left, key);
            } else {
                return true;
            }
        }

        //二叉树移出一个元素
        removeNode(key) {

            this._removeNode(this.root, key);

        }

        _removeNode(node, key) {

            if (node === null) {
                return;
            }

            if (key < node.key) {

                node.left = this._removeNode(node.left, key);
                return node;

            } else if (key > node.key) {

                node.right = this._removeNode(node.right, key);
                return node;

            } else {
                //第一种情况------一个节点
                if (node.left === null && node.right === null) {
                    node = null;
                    return node;
                }

                //第二种情况-----一个只有一个子节点的节点
                if (node.left === null) {
                    node = node.right;
                    return node;
                } else if (node.right === null) {
                    node = node.left;
                    return node;
                }

                //第三种情况-----一个有两个子节点的节点
                let aux = this._findMinNode(node.right);
                node.key = aux.key;
                node.right = this._removeNode(node.right, aux.key);
                return node;
            }
        }


        _findMinNode(node) {

            while (node && node.left !== null) {
                node = node.left;
            }
            return node;
        };
    }

    class Node {

        constructor(key) {
            this.key = key;
            this.left = null;
            this.right = null;
        }
    }

    let tree = new BinarySearchTree();

    tree.insert(11);
    tree.insert(7);
    tree.insert(15);
    tree.insert(5);
    tree.insert(9);
    tree.insert(8);
    tree.insert(10);
    tree.insert(13);
    tree.insert(12);
    tree.insert(14);
    tree.insert(20);
    tree.insert(18);
    tree.insert(25);
    tree.insert(6);
    tree.insert(2);
    tree.insert(20);
    console.log(tree);

    function printNode(val) {
        console.log(val);
    }

    tree.inOrderTraverse(printNode);


    console.log(tree.min());
    console.log(tree.max());

    console.log(tree.search(6));

}